using System;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;

namespace NNews
{
	public class Server
	{		
		public const int DefaultPort = 119;
		
		public Server(IPAddress address)
			: this(address, DefaultPort) {}
		
		public Server(IPAddress address, int port)
		{
			Address = address;
			Port = port;
			
			ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
		}

		public void Connect() {
			IPEndPoint endPoint = new IPEndPoint(Address, Port);			
			ClientSocket.Connect(endPoint);			
		}
		
		private Socket clientSocket;			
		private IPAddress address;
		private int port;
		
		protected Socket ClientSocket {
			get { return clientSocket; }
			set { clientSocket = value; }
		}		
		
		public int Port {
			get { return port; }
			set { port = value; }
		}
		
		public IPAddress Address {
			get { return address; }
			set { address = value; }
		}			
		
		public bool Connected {
			get { return clientSocket.Connected; }
		}
		
		public override bool Equals(object obj)
		{
			Server other = obj as Server;
			return (other == null) ? false : other.Address == Address;
		}
		
		public override int GetHashCode()
		{
			return Address.GetHashCode();
		}
	}
}
